<?php

define('ACC',true);

require ('../includes/init.php');
require ('left.php');

if(isset($_SESSION['user_id']) && isset($_SESSION['username'])){
	$smarty->assign('SESSION', $_SESSION);
}

$user=new UserModel();
$smarty->assign('ur_here', '当前位置:&nbsp;<a href=".">首页</a><code>&nbsp;&gt;&nbsp;</code>&nbsp;系统提示');

if($_GET['act'] == 'login'){

	$smarty->assign('action', 'log');
	$smarty->display('./front/user_passport.html');
	exit;

}else if($_GET['act'] == 'login_ok'){
	
	if(isset($message)){
		$message = $_POST['message'];
	}else{
		$message = 0;
	}
	
	$username=trim($_POST['username']);
	$password=trim($_POST['passwd']);

	if(!$username || mb_strlen($username)<4 || mb_strlen($username)>16){
		$smarty->assign('action', 'return');
		$smarty->assign('url', 'javascript:history.back()');
		msg($smarty, '请检验用户名！');
	}
	if(!$password){
		$smarty->assign('action', 'return');
		$smarty->assign('url', 'javascript:history.back()');
		msg($smarty, '密码不能为空！');
	}
	
	$userInfo=$user->checkUser($username,$password);

	if($userInfo == false){
		$smarty->assign('action', 'return');
		$smarty->assign('url', 'javascript:history.back()');
		msg($smarty, '用户名或密码不正确！');
	}

	$user->upLog($userInfo['user_id']);
	
	if($message == 1){
		setcookie('username', $username, time()+3600);
	}

	$_SESSION['user_id']=$userInfo['user_id'];
	$_SESSION['username']=$username;

	$smarty->assign("SESSION",$_SESSION);
	$smarty->assign('action', 'return');
	$smarty->assign('url', 'index.php');
	msg($smarty,'登录成功');

}else if($_GET['act'] == 'register'){

	$smarty->assign('action', 'reg');
	$smarty->display("./front/user_passport.html");
	exit;

}else if($_GET['act'] == 'reg_ok'){

	$smarty->assign('action', 'return');
	$smarty->assign('url', 'javascript:history.back()');

	if(isset($_GET['val']) && (($val = trim($_GET['val'])) != '')){
		if(isset($_SESSION['imageStr']) && strtolower($val) == strtolower($_SESSION['imageStr'])){
			echo 1;
			return;
		}else{
			echo 0;
			return;
		}
	}
	if(($code = trim($_POST['checkCode'])) != ''){
		if(!isset($_SESSION['imageStr']) || (strtolower($code) != strtolower($_SESSION['imageStr']))){
			msg($smarty,'验证码错误！');
			exit;
		}
	}else{
		msg($smarty,'验证码错误！');
		exit;
	}
	$data['username']=trim($_POST['username']);
	$data['password']=trim($_POST['password']);
	$data['email']=trim($_POST['email']);

	$rePassWord=trim($_POST['rePassWord']);
	$checkCode=trim($_POST['checkCode']);

	$patt='/^[\w\x{4E00}-\x{9FA5}]{4,16}$/u';
	if(!preg_match($patt,$data['username'])){
		msg($smarty,'用户名长度应在4-16位');
	}

	if($data['password']!=$rePassWord){
		msg($smarty,'两次密码不一致！');
	}
	if(empty($data['password'])){
		msg($smarty,'密码不能为空！');
	}
	$patt='/^[a-z0-9]\w*(\.[a-z0-9]+)*@[a-z0-9][a-z0-9-]*(\.[a-z0-9][a-z0-9-]*)+$/i';
	if(!preg_match($patt,$data['email'])){
		msg($smarty,'请填写正确的email');
	}

	$data['regtime']=time();

	$user=new UserModel();
	if($user->checkUser($data['username'])){
		msg($smarty,'用户名已存在！');
	}

	if($user->reg($data)){
		$smarty->assign('action', 'login');
		$smarty->assign('url', 'user.php?act=login');
		msg($smarty,'注册成功！');
	}else{
		msg($smarty,'注册失败！');
	}

}else if($_GET['act'] == 'log_out'){

	$_SESSION['userName']=null;
	session_destroy();

	$smarty->assign('action', 'index');
	$smarty->assign('url', 'index.php');
	msg($smarty,'退出成功');

}



if(!isset($_SESSION['user_id']) || !isset($_SESSION['username'])){
	$smarty->assign('ur_here', '当前位置:&nbsp;<a href=".">首页</a><code>&nbsp;&gt;&nbsp;</code>&nbsp;系统提示');
	$smarty->assign('action', 'login');
	$smarty->assign('url', 'user.php?act=login');
	msg($smarty, '请先登录！');
}

if($_GET['act'] == 'manage'){
	
	$lastLogin = $user->get_login_time($_SESSION['user_id']);
	$lastLogin = date('Y-m-d H:i:s', $lastLogin);
	
	$smarty->assign('lastLogin', $lastLogin);
	$smarty->assign('action', 'index');
	$smarty->assign('SESSION', $_SESSION);
	$smarty->display('./front/user_manage.html');
	exit;
	
}

if($_GET['act'] == 'address_insert'){
	
	$data['user_id'] = $_SESSION['user_id'];
	$data['country'] = trim($_POST['country']);
	$data['province'] = trim($_POST['province']);
	$data['city'] = trim($_POST['city']);
	$data['district'] = trim($_POST['district']);
	$data['consignee'] = trim($_POST['consignee']);
	$data['email'] = trim($_POST['email']);
	$data['address'] = trim($_POST['address']);
	$data['zipcode'] = $_POST['zipcode'] + 0;
	$data['tel'] = $_POST['tel'] + 0;
	$data['mobile'] = $_POST['mobile'] + 0;
	$user -> regionInsert($data, 'user_address');
	header("Location: user.php?act=address_list");
	
}

if($_GET['act'] == 'address_list'){
	
	$address_list = $user->get_address_list($_SESSION['user_id']);
//	print_r($address_list);exit;
	$smarty->assign('address_list', $address_list);
	$smarty->assign('action', 'address');
	$smarty->display('./front/user_manage.html');
	exit;
	
}

if($_GET['act'] == 'drop_consignee'){
	
	$address_id = $_GET['id'] + 0;
	$user->drop_consignee($address_id);
	header("Location: user.php?act=address_list");
	
}

if($_GET['act'] == 'order_list'){
	
	$order_list = $user->get_order_list($_SESSION['user_id']);
	$count = $user->get_order_count();
	foreach($order_list as $key => $val){
		$order_list[$key]['addtime'] = date('Y-m-d H:i:s', $val['addtime']);
	}
	
	$smarty->assign('count', $count);
	$smarty->assign('order_list', $order_list);
	$smarty->assign('action', 'order_list');
	$smarty->display('./front/user_manage.html');
	exit;
	
}

if($_GET['act'] == 'profile'){
	
	$user_info = $user->get_user_info($_SESSION['user_id']);
	
	$smarty->assign('user_info', $user_info);
	$smarty->assign('action', 'profile');
	$smarty->display('./front/user_manage.html');
	exit;
	
}

if($_GET['act'] == 'user_update'){
	
	$data = array();
	$user_id = $_GET['user_id'] + 0;
	$data['username'] = trim($_POST['username4']);
 	$sex = $_POST['sex'] + 0;
	if($sex == 1){
		$data['sex'] = '男';
	}else if($sex == 2){
		$data['sex'] = '女';
	}
	$data['email'] = trim($_POST['email']);
	$data['qq'] = $_POST['qq'] + 0;
	$data['tel'] = $_POST['tel'] + 0;
	
	$birthdayYear = $_POST['birthdayYear'] + 0;
	$birthdayMonth = $_POST['birthdayMonth'] + 0;
	$birthdayDay = $_POST['birthdayDay'] + 0;
	if($birthdayYear != 0 && $birthdayMonth != 0 && $birthdayDay != 0){
		$data['birthday'] = $birthdayYear . '-' . $birthdayMonth . '-' . $birthdayDay;
	}
	
	$where = 'user_id = ' . $_SESSION['user_id'];
	if($user->user_update($data, $where)){
		$smarty->assign('ur_here', '当前位置:&nbsp;<a href=".">首页</a><code>&nbsp;&gt;&nbsp;</code>&nbsp;系统提示');
		$smarty->assign('action', 'return');
		$smarty->assign('url', 'javascript:history.back()');
		msg($smarty, '修改信息成功');
	}else{
		$smarty->assign('ur_here', '当前位置:&nbsp;<a href=".">首页</a><code>&nbsp;&gt;&nbsp;</code>&nbsp;系统提示');
		$smarty->assign('action', 'return');
		$smarty->assign('url', 'javascript:history.back()');
		msg($smarty, '修改信息失败');
	}

}

if($_GET['act'] == 'password_update'){
	$old_password = trim($_POST['ol_password']);
	$password = $user->get_password($_SESSION['user_id']);
	$comfirm_password = trim($_POST['comfirm_password']);

	if(md5($old_password) == $password){
		$new_password = trim($_POST['new_password']);
		
		if($new_password == ''){
			$smarty->assign('ur_here', '当前位置:&nbsp;&gt;&nbsp;系统提示');
			$smarty->assign('action', 'return');
			$smarty->assign('url', 'javascript:history.back()');
			msg($smarty,'密码不能为空');
		}
		if($new_password != $comfirm_password || $comfirm_password == ''){
			$smarty->assign('ur_here', '当前位置:&nbsp;&gt;&nbsp;系统提示');
			$smarty->assign('action', 'return');
			$smarty->assign('url', 'javascript:history.back()');
			msg($smarty,'确认密码填写错误');
		}
		if($user->password_update($_SESSION['user_id'],$new_password)){
			$smarty->assign('ur_here', '当前位置:&nbsp;&gt;&nbsp;系统提示');
			$smarty->assign('action', 'return');
			$smarty->assign('url', 'javascript:history.back()');
			msg($smarty,'密码修改成功');
		}else{
			$smarty->assign('ur_here', '当前位置:&nbsp;&gt;&nbsp;系统提示');
			$smarty->assign('action', 'return');
			$smarty->assign('url', 'javascript:history.back()');
			msg($smarty,'密码修改失败');
		}
	}else{
		$smarty->assign('ur_here', '当前位置:&nbsp;&gt;&nbsp;系统提示');
		$smarty->assign('action', 'return');
		$smarty->assign('url', 'javascript:history.back()');
		msg($smarty,'密码填写错误');
	}
	
}

if($_GET['act'] == 'address_update'){
	
	$address_id = $_GET['address_id'] + 0;
	$user_address = $user->get_user_address($address_id);
	
	$smarty->assign('address', $user_address);
	$smarty->assign('action', 'address_update');
	$smarty->display('./front/user_manage.html');
	exit;

}

if($_GET['act'] == 'address_update_ok'){

	$address_id = $_GET['address_id'] + 0;
	
	print_r($_POST);exit;
	
	$data['country'] = trim($_POST['country']);
	$data['province'] = trim($_POST['province']);
	$data['city'] = trim($_POST['city']);
	$data['district'] = trim($_POST['district']);
	$data['consignee'] = trim($_POST['consignee']);
	$data['email'] = trim($_POST['email']);
	$data['address'] = trim($_POST['address']);
	$data['zipcode'] = $_POST['zipcode'] + 0;
	$data['tel'] = $_POST['tel'] + 0;
	$data['mobile'] = $_POST['mobile'] + 0;

	$where = ' address_id = ' . $address_id ;
	if($user -> addressUpdate($data, 'user_address', $where)){
		header("Location: user.php?act=address_list");
	}

}

if($_GET['act'] == 'delete_order'){
	
	$order_id = $_GET['order_id'] + 0;
	if($user->delete_order($order_id)){
		header("Location: user.php?act=order_list");
	}
	
}

if($_GET['act'] == 'order_detail'){
	
	$order_id = $_GET['order_id'] + 0;
	
	$order_info = $user->get_order_info($order_id);
	
	$region = '';
	if(isset($order_info['country'])){
		$region .= $order_info['country'] . '&nbsp;';
	}
	if(isset($order_info['province'])){
		$region .= $order_info['province'] . '&nbsp;';
	}
	if(isset($order_info['city'])){
		$region .= $order_info['city'] . '&nbsp;';
	}
	if(isset($order_info['district'])){
		$region .= $order_info['district'] . '&nbsp;';
	}
	if(isset($order_info['address'])){
		$region .= $order_info['address'];
	}
	
	$goods_list = $user->get_goods_list($order_id);
	if(!($shipping_price = $user->get_shipping_price($order_info['shipping_id']))){
		$shipping_price = 0.00;
	}
	$subtotal = 0;
	foreach($goods_list as $key => $val){
		$subtotal += $val['subtotal'];
	}

	$smarty->assign('region', $region);
	$smarty->assign('subtotal', $subtotal);
	$smarty->assign('goods_list', $goods_list);
	$smarty->assign('order_info', $order_info);
	
	$smarty->assign('action', 'order_detail');
	$smarty->display('./front/user_manage.html');
	exit;
	
}












